<!--
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-28 16:21:26
 * @LastEditTime : 2019-12-20 15:02:18
 * @LastEditors  : Please set LastEditors
 -->
<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../../css/vant.css">
    <link rel="stylesheet" type="text/css" href="../../css/base.css" />
    <script type="text/javascript" src="../../script/jquery.js"></script>
    <script type="text/javascript" src="../../script/layer/mobile/layer.js"></script>
    <script type="text/javascript" src="../../script/vue.min.js"></script>
    <script type="text/javascript" src="../../script/vant.min.js"></script>
    <script type="text/javascript" src="../../script/fastclick.js"></script>
    <script type="text/javascript" src="../../script/base.js"></script>
    <style type="text/css">
        .content {
            background: #fff !important;
        }

        .ul,
        li {
            padding: 0;
            margin: 0;
            list-style: none;
            display: block;
        }

        /* li:active {
            background: #e2e2e2;
        } */

        .row {
            position: relative;
        }

        .header {
            width: 55px;
            height: 55px;
            padding: 10px;
        }

        .header img {
            width: 100%;
            height: 100%;
            border-radius: 4px;
            overflow: hidden;
        }

        .label {
            position: absolute;
            top: 0;
            left: 55px;
            right: 0;
            height: 55px;
            border-bottom: 1px solid #efefef;
            padding: 10px 0;
        }

        .label .name {
            line-height: 34px;
            font-size: 16px;
            padding-right: 20px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            position: relative;
        }

        .label .name .time {
            position: absolute;
            top: 0;
            right: 0;
            padding-right: 15px;
            line-height: 26px;
            font-size: 12px;
            color: #a2a2a2;
        }

        .label .text {
            line-height: 19px;
            font-size: 14px;
            color: #a2a2a2;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            padding-right: 15px;
        }

        .b {
            height: 10px;
            background: #ededed;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>


        <div class="box">
            <van-nav-bar-main title="聊天列表" left-arrow @click-left="onClickLeft">
                <div slot="right" class="head-right" v-show="isInvitation">
                    <span @click="completeAdd" style="color:#000;">完成</span>
                </div>
            </van-nav-bar-main>
            <div class="content">
                <div>
                    <ul>
                        <li @click="openGroupChat" v-show="!isInvitation">
                            <div class="row">
                                <div class="header">
                                    <img src="../../image/ql.png" />
                                </div>
                                <div class="label" style="border: 0;">
                                    <div class="name">群聊</div>
                                    <div class="text">
                                        <!-- <div v-html="returnHtml(row)"></div> -->
                                    </div>
                                </div>
                            </div>
                        </li>
                        <li class="b" v-show="!isInvitation"></li>
                        <li v-for="(row,index) in friendList" @click="intoDetail(row,index)">
                            <div class="row">
                                <div class="header">
                                    <img :src="row.avatar?row.avatar:'../../image/we_header.png'" />
                                </div>
                                <div class="label">
                                    <div class="name">{{row.username}}
                                        <div style="float: right;" v-show="isInvitation">
                                            <van-checkbox v-model="row.checked" shape="round" slot="right-icon"
                                                style="margin-top: 6px;" />
                                        </div>

                                    </div>
                                    <!-- <div class="text">{{row.sign}}</div> -->
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- <div class="init-full">
            <van-nav-bar class="head" title="聊天列表" left-arrow @click-left="onClickLeft">
                <div slot="right" class="head-right" v-show="isInvitation">
                    <span @click="completeAdd" style="color:#000;">完成</span>
                </div>
            </van-nav-bar>
            <div class="body-no-bottom">
                <div class="content">
                    <div>
                        <ul>
                            <li @click="openGroupChat" v-show="!isInvitation">
                                <div class="row">
                                    <div class="header">
                                        <img src="../../image/ql.png" />
                                    </div>
                                    <div class="label" style="border: 0;">
                                        <div class="name">群聊</div>
                                        <div class="text">
                                        </div>
                                    </div>
                                </div>
                            </li>
                            <li class="b" v-show="!isInvitation"></li>
                            <li v-for="(row,index) in friendList" @click="intoDetail(row,index)">
                                <div class="row">
                                    <div class="header">
                                        <img :src="row.avatar?row.avatar:'../../image/we_header.png'" />
                                    </div>
                                    <div class="label">
                                        <div class="name">{{row.username}}
                                            <div style="float: right;" v-show="isInvitation">
                                                <van-checkbox v-model="row.checked" shape="round" slot="right-icon"
                                                    style="margin-top: 6px;" />
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
                <van-loading v-if="showLoading" />
            </div>
        </div> -->


        <van-dialog v-model="showCreate" show-cancel-button close-on-click-overlay confirm-button-text="确认"
            cancel-button-text="取消" @confirm="approvalConfirm(1)" @cancel="approvalConfirm(0)">
            <div style="padding-top:10px;">
                <van-field label="群聊名称" v-model="groupName" placeholder="请输入群聊名称"></van-field>
            </div>
        </van-dialog>
    </div>
</body>

<script type="text/javascript">
    var vm;
    apiready = function () {

        var isInvitation = api.pageParam.isInvitation || 0;

        vm = new Vue({
            el: '#app',
            data: {
                showLoading: false,
                friendList: [],
                groupList: [],
                isInvitation: isInvitation,
                showCreate: false,
                groupName: ''
            },
            created: function () {
                var that = this;
                that.getFriendList();
            },
            methods: {
                getFriendList: function () {
                    var that = this;
                    APP.ajax('GetChatUser', { userID: APP.getUserInfo().UserID }, function (res) {
                        var friends = [];
                        for (var i = 0; i < res.ds1.length; i++) {
                            friends.push({
                                username: res.ds1[i].UserName,
                                id: res.ds1[i].UserID.toString(),
                                status: res.ds1[i].ContextIDs ? "online" : 'offline',
                                sign: res.ds1[i].Sign,
                                avatar: res.ds1[i].HeadImg,
                                contextIDs: res.ds1[i].ContextIDs
                            })
                        }
                        that.friendList = friends;

                        var groups = [];
                        for (var i = 0; i < res.ds2.length; i++) {
                            groups.push({
                                groupname: res.ds2[i].GroupName,
                                id: res.ds2[i].GroupID,
                                avatar: "../image/group.png" //群组头像
                            })
                        }
                        that.groupList = groups;
                    })
                },
                completeAdd: function () {
                    var that = this;
                    var checkArray = [];
                    for (var i in that.friendList) {
                        if (that.friendList[i].checked) {
                            checkArray.push(that.friendList[i])
                        }
                    }
                    if (checkArray.length) {
                        var userIDArray = [];
                        for (var i in checkArray) {
                            userIDArray.push(checkArray[i].id);

                        }
                        if (that.isInvitation == 1) {
                            api.sendEvent({
                                name: 'joinGroup',
                                extra: {
                                    userIDArray: userIDArray
                                }
                            });
                            api.closeWin();
                        } else {
                            that.showCreate = true;

                        }


                    }
                },
                approvalConfirm: function (index) {
                    var that = this;
                    if (index == 0) {
                        that.groupName = '';
                        that.showCreate = false;
                    } else {
                        var userIDArray = [];
                        var checkArray = [];
                        for (var i in that.friendList) {
                            if (that.friendList[i].checked) {
                                checkArray.push(that.friendList[i])
                            }
                        }
                        for (var i in checkArray) {
                            userIDArray.push(checkArray[i].id);

                        }
                        api.sendEvent({
                            name: 'newGroup',
                            extra: {
                                userIDArray: userIDArray,
                                groupName: that.groupName
                            }
                        });
                        api.closeWin();
                    }
                },
                openGroupChat: function () {
                    var that = this;
                    api.openWin({
                        name: 'chatGroupList',
                        url: 'chatGroupList.html',
                        reload: true,
                        slidBackEnabled: false,
                        allowEdit:true,
                        pageParam: {
                            groupList: that.groupList
                        }
                    })
                },
                intoDetail: function (row, index) {
                    var that = this;
                    if (that.isInvitation) {
                        if (typeof row.checked === 'undefined') {
                            this.$set(row, 'checked', true);
                        } else {
                            row.checked = !row.checked;
                        }
                    } else {
                        row.type = 'friend';
                        api.openWin({
                            name: 'chatListDetail',
                            url: 'chatListDetail.html',
                            reload: true,
                            slidBackEnabled: false,
                            allowEdit:true,
                            pageParam: {
                                row: row
                            }
                        })
                    }


                },
                onClickLeft: function () {
                    api.closeWin();
                }
            }
        })
    }
</script>

</html>